package com.vvlive.caibao.smartcity.ui;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.vvlive.caibao.smartcity.base.BaseActivity;
import com.vvlive.caibao.smartcity.utils.PhoneNumsUtils;
import com.vvlive.caibao.smartcity.utils.SMSSDKUtil;
import com.vvlive.caibao.smartcity.view.CommonActionBar;

import org.kymjs.kjframe.ui.BindView;

import smartcity.vvlive.caibao.com.smartc.R;

/**
 * Created by android on 2015/7/4.
 * 注册页面
 */
public class CreateAccountAtivity extends BaseActivity implements View.OnClickListener {

    @BindView(id= R.id.card_number)
    private EditText mCard_number;
    @BindView(id= R.id.phone_number)
    private EditText mPhone_number;
    @BindView(id= R.id.identity_code)
    private EditText mCode;
    @BindView(id= R.id.access_code,click = true)
    private Button mAccess_code;
    @BindView(id= R.id.input_pass)
    private EditText mInput_pass;
    @BindView(id= R.id.reader_)
    private CheckBox mCheckBox;
    @BindView(id= R.id.terms)
    private TextView mTems;
    @BindView(id= R.id.at_once_create,click = true)
    private Button mAt_once;
    public static final int COUNT_DOWN = 0x001;
    public static final int GET_CODE=0x003;
    private Context context;
    SMSSDKUtil smssdkUtil;
    @Override
    protected int getContentViewRes() {
        return R.layout.activity_create_account;
    }

    @Override
    public void initWidget() {
        super.initWidget();
        CommonActionBar commonActionBar = getCommonActionBar();
        commonActionBar.setMoreVisible(false);
        commonActionBar.setTitle(R.string.create_account);
        context = this;
        smssdkUtil = new SMSSDKUtil(context,handler);
        //SMSSDK.initSDK(this,"9997f26d9c62", "428d053f88f9cf68d6a3fd2b125945ad");
       smssdkUtil.eventHandler();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.access_code:
                if(!PhoneNumsUtils.judgePhoneNums(mPhone_number.getText().toString())){
                    return;
                }
                smssdkUtil.Countdown();
                smssdkUtil.senPhone(mPhone_number.getText().toString());
                mAccess_code.setClickable(false);
                mAccess_code.setBackgroundColor(getResources().getColor(R.color.gery));
                break;
            case R.id.at_once_create:
                if (Judge()){
                    alert("注册成功");
                    smssdkUtil.submitVerificationCode( mPhone_number.getText().toString(), mCode.getText().toString());
                }else {
                    alert("请完善注册信息");
                }
                break;
            default:
                break;
        }
    }
    /*int time= 20;
    private Timer mTimer;
    private TimerTask mTask;
    private void  Countdown() {
        mTimer = new Timer();
        mTask = new TimerTask() {
            @Override
            public void run() {
                if (time>0){
                  handler.sendMessage(handler.obtainMessage(COUNT_DOWN,time));
                }else if(time==0){
                    handler.sendMessage(handler.obtainMessage(COUNT_DOWN+1,time));
                    time = 20;
                    mTask.cancel();
                }
                time--;
            }
        };
        mTimer.schedule(mTask, 0, 1000);
    }*/
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case COUNT_DOWN:
                    mAccess_code.setText((int)msg.obj+"秒");
                    break;
                case COUNT_DOWN+1:
                    mAccess_code.setText("重新获取");
                    mAccess_code.setBackgroundColor(getResources().getColor(R.color.orange_red));
                    mAccess_code.setClickable(true);
                    break;
                case GET_CODE:
                    smssdkUtil.sendCode(msg.arg1,msg.arg2,msg.obj,null);
                       //sendCode(msg.arg1,msg.arg2,msg.obj,new Throwable());
                    break;
                default:
                    break;
            }
        }
    };


   /* *//**
     * MOB 回调
     *
     * *//*
    private void MOBsdk(){
        EventHandler eventHandler = new EventHandler(){
            @Override
            public void afterEvent(int i, int i1, Object o) {
                super.afterEvent(i, i1, o);
                handler.sendMessage(handler.obtainMessage(GET_CODE, i,  i1,  o));
            }
        };
        //注册回调监听接口
        SMSSDK.registerEventHandler(eventHandler);
    }

    *//**
     * 发送验证的码
     * *//*
    Class aClass;
    private void sendCode(int event,int result,Object obj,Throwable data){
        if (SMSSDK.RESULT_COMPLETE==result){
            if (SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE==event){
                //在这里执行跳转页面
                showActivity((Activity) context, aClass);
            }else if (SMSSDK.EVENT_GET_VERIFICATION_CODE==event){
                alert("发送验证码，请稍后。。");
            }else {
                (data).printStackTrace();
            }
        }else if (SMSSDK.RESULT_ERROR==result) {
            alert("验证码错误");
        }
    }
*/

    private boolean Judge(){
        return !TextUtils.isEmpty(mCard_number.getText().toString())&&!TextUtils.isEmpty(mPhone_number.getText().toString())&&!TextUtils.isEmpty(mCode.getText().toString())&&
             !TextUtils.isEmpty(mInput_pass.getText().toString())&&mCheckBox.isChecked()? true:false;
    }

}
